import { createRouter, createWebHashHistory } from 'vue-router'
import routes from '@/router/routes'
import routerNames from '@/router/routerNames'
import {userLoginStore} from '@/store/userLogin'

const router = createRouter({
  scrollBehavior: (to, from, savedPosition) => {
    return { top: 0 }
  },
  history: createWebHashHistory(),
  routes: routes
})

router.beforeEach((to, from) => {
  const loginStore = userLoginStore()
  if (to.name !== routerNames.login && !loginStore.token){
    return {name: routerNames.login}
  }
})

router.afterEach((to) => {
  // 从路由的 meta 中获取标题，若不存在则使用默认值
})

export default router